package com.apes.framework.plugin.esb.parser.node;

import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.EsbRouter;
import com.apes.framework.plugin.event.EventConst;
import com.apes.framework.plugin.event.EventService;
import com.apes.framework.plugin.filereader.xml.AbstractNodeParser;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.EventUtil;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

/**
 * 功能：chain 节点解析器
 *
 * @author xul
 * @create 2018-04-18 11:02
 */
@Component
public class ChainNodeParser extends AbstractNodeParser {
    /**
     * 可解析的节点列表
     **/
    private final String[] nodeNames = {"chain", "consumer"};

    /**
     * 可解析节点域
     **/
    public static final String domain = "esb";

    /**
     * 功能：解析节点
     *
     * @param node
     */
    @Override
    public ApesElement parse(Element node) {
        ApesElement element = super.parse(node);
        if(element == null) return null;
        if (element.getName().equals("consumer")) {
            subscribe(element);
        } else if (element.getName().equals("chain")) {
            EsbRouter.setRouter(element.getAttribute("method").toString(), element.getAttribute("version").toString(), element);
            SpringManager.getBean(EventService.class).sendSynEvent(EventConst.EVENT_CHAIN_NODE_PARSER, element);
        }
        return element;
    }

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return domain;
    }

    /**
     * 功能：可解析的节点列表
     */
    @Override
    public String[] getNames() {
        return nodeNames;
    }

    @Override
    public void setNestedElement(ApesElement element, ApesElement nestedElement) {
        if (nestedElement.getName().equals("consumer")) {
            subscribe(nestedElement);
        } else {
            super.setNestedElement(element, nestedElement);
        }
    }

    @Override
    protected void setDefaultValue(ApesElement element) {
        super.setDefaultValue(element);
        if (element.getName().equalsIgnoreCase("subChain")) {
            if (!element.getAttributes().containsKey("version")) {
                element.setAttribute("version", ApesConst.METHOD_VERSION);
            }
        }
        if (element.getName().equalsIgnoreCase("chain")) {
            if (!element.getAttributes().containsKey("version")) {
                element.setAttribute("version", ApesConst.METHOD_VERSION);
            }
            if (!element.getAttributes().containsKey("needInSession")) {
                element.setAttribute("needInSession", "true");
            }
            if (!element.getAttributes().containsKey("isTransaction")) {
                element.setAttribute("isTransaction", "true");
            }
        }
    }

    protected void subscribe(ApesElement nestedElement) {
        EventUtil.sendAynEvent(this, "event:mq.subscribe", nestedElement);
    }
}
